package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.common.TimeUtils;
import com.zjc.pojo.AbsentTable;
import com.zjc.pojo.Repair;
import com.zjc.pojo.Student;
import com.zjc.pojo.VisitTable;
import com.zjc.service.DromAdminService;
import com.zjc.service.RepairService;
import com.zjc.service.StudentService;
import com.zjc.service.VisitTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/repair")
public class RepairController {
	@Autowired
	RepairService repairService;

	@Autowired
	StudentService studentService;
	@Autowired
	VisitTableService visitTableService;
	@Autowired
	DromAdminService dromAdminService;
	@GetMapping("/find")
	public R<Page> GetRepairPage(int pageNum, int pageSize, String isFinsih,String stuName,String roomId,String rpStatus){
		QueryWrapper<Repair> wrapper = new QueryWrapper<>();

		if(isFinsih!=null){
			wrapper.eq("is_finish",Integer.valueOf(isFinsih));
		}
		if(stuName!=null){
			wrapper.like("stu_name",stuName);
		}
		if(roomId!=null){
			wrapper.eq("room_id",Integer.valueOf(roomId));
		}
		if(rpStatus!=null){
			wrapper.eq("rp_status",Integer.valueOf(rpStatus));
		}
		Page<Repair> page1 = new Page<>(pageNum,pageSize);
		repairService.page(page1,wrapper);
		return R.success(page1);
	}
	@PutMapping("/updateStatus")
	public R<String> updateStatus(@RequestBody Repair repair){
		repair.setRpStatus(true);
		repairService.updateById(repair);
		return R.success("修改成功");
	}
	//获取所有记录条数
	@GetMapping("/total")
	public R<Long> gettotal(){
		LambdaQueryWrapper<Repair> lambdaQueryWrapper =new LambdaQueryWrapper<>();
		Long num = repairService.count(lambdaQueryWrapper);
		return  R.success(num);
	}
	//获取今日新增条数
	@GetMapping("/todayadd")
	public R<Long> todayadd() throws ParseException {
		LambdaQueryWrapper<Repair> lambdaQueryWrapper =new LambdaQueryWrapper<>();
		Date date = new Date();
		//时间格式转换
		date = TimeUtils.getSqlDate(date);
		lambdaQueryWrapper.eq(Repair::getRpDate,date);
		Long num = repairService.count(lambdaQueryWrapper);
		return R.success(num);
	}

	@GetMapping("/findByStuId")
	public  R<List<Repair>> RpFindByStuid(Integer stuId){
		LambdaQueryWrapper<Repair> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Repair::getStuId,stuId);
		List<Repair> lists = repairService.list(lambdaQueryWrapper);
		return R.success(lists);
	}
	@PostMapping("/applyadd")
	public R<String> AddApply(@RequestBody Repair at) throws ParseException {

		Integer roomid = at.getRoomId();
		Integer stuid = at.getStuId();
		String stuname = at.getStuName();
		Integer ldid = at.getLdId();

		if(!studentService.isStuExists(stuid)){
			return R.error("学号错误！！");
		}
		if(!dromAdminService.isExistsLd(ldid)){
			return R.error("楼栋不存在！！");
		}
		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,stuname)){
			return R.error("姓名学号不匹配！");
		}
		LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Student::getStuId,stuid);
		Student student = studentService.getOne(lambdaQueryWrapper);
		if(!student.getRoomId().equals(roomid)){
			return R.error("房间号有误！");
		}
		Date date = new Date();
		date = TimeUtils.getSqlDate(date);
		at.setRpDate(date);
		repairService.save(at);
		return R.success("添加申请成功");
	}
	@PutMapping("/updateFinish")
	public  R<String> updateFinish(@RequestBody Repair repair){
		Integer rpid = repair.getRpId();
		Repair rp = repairService.getById(rpid);
		rp.setIsFinish(true);
		repairService.updateById(rp);
		return R.success("修改成功");
	}

}

